package com.sxj.corejava.code07_面向对象.bms;

import java.util.Scanner;

/**
 * @author 石小俊
 * @date 2025年01月08日
 * 银行主菜单,该页面无法直接运行,必须通过用户登录之后进入
 */
public class BankMenu {

    private Scanner sc = new Scanner(System.in);

    // 由于银行主菜单必须指定是哪个账户的
    // 因此,可以提供一个构造方法,要求参数必须是账户对象
    private Account account;

    public Account getAccount() {
        return account;
    }

    public void setAccount(Account account) {
        this.account = account;
    }

    public BankMenu(Account account) {
        this.account = account;
    }

    public void showMenu() {
        // 进入该菜单,必须指明是哪个账户的
        System.out.println("****************************欢迎" + account.getName() + "进入银行主菜单****************************");
        System.out.print("请选择服务(1.存款 2.取款 3.查询余额 4.转账 5.贷款 6.还款 7.查询贷款金额 8.修改密码 9.退出登录):");
        // 每一个服务对应一个实现的方法
        int choice = sc.nextInt();
        switch (choice) {
            case 1:
                deposit();
                break;
            case 2:
                withdraw();
                break;
            case 3:
                queryBalance();
                break;
            case 4:
                transfer();
                break;
            case 5:
                providerLoan();
                break;
            case 6:
                payLoan();
                break;
            case 7:
                queryLoan();
                break;
            case 8:
                modifyPassword();
                break;
            case 9:
                logout();
                break;
            default:
                System.out.println("错误:您输入的选项有误,请重新输入!");
                showMenu();
        }
    }

    public void deposit() {
        System.out.println("*************************开始存款*************************");
        System.out.print("请输入存款金额:");
        double money = sc.nextDouble();
        // 调用存款方法
        account.deposit(money);
        System.out.println("提示:恭喜您,存款成功!");
        showMenu();
    }

    public void withdraw() {
        System.out.println("*************************开始取款*************************");
        System.out.print("请输入取款金额:");
        double money = sc.nextDouble();
        // 调用取款方法实现取款功能
        boolean flag = account.withdrawMoney(money);
        if (flag) {
            System.out.println("提示:恭喜您,取款成功!");
        } else {
            System.out.println("错误:对不起,您的账户余额不足,取款失败!");
        }
        showMenu();
    }

    public void queryBalance() {
        System.out.println("*************************查询余额*************************");
        System.out.println("提示:您当前账户余额为:" + account.getBalance() + "元");
        showMenu();
    }

    public void transfer() {
        System.out.println("*************************开始转账*************************");
        System.out.print("请输入要转入的目标账户的卡号:");
        int id = sc.nextInt();

        // 调用银行提供的方法根据卡号找到目标账户
        Account targetAccount = Bank.findById(id);
        // 判断账户是否存在
        if (account == null) {
            // 如果账户不存在,则无法转账
            System.out.println("错误:目标账户不存在,请检查卡号是否正确!");
        } else {
            // 如果账户存在,则实现转账流程
            System.out.print("请输入转账金额:");
            double money = sc.nextDouble();
            // 判断当前账户类型是储蓄账户还是信用账户
            if (account instanceof SavingAccount) {
                // 判断当前账户余额是否不低于转账金额
                if (account.getBalance() >= money) {
                    // 如果不低于,则进行转账
                    // 1.当前账户余额减少
                    account.setBalance(account.getBalance() - money);
                    // 2.目标账户余额增加
                    targetAccount.setBalance(targetAccount.getBalance() + money);
                    System.out.println("提示:恭喜您,向" + id + "账户成功转入" + money + "元");
                } else {
                    // 如果账户余额不足,则报错
                    System.out.println("错误:对不起,您的账户余额不足,转账失败!");
                }
            } else {
                // 判断当前账户余额是否不低于转账金额
                CreditAccount creditAccount = (CreditAccount) account;
                if (creditAccount.getBalance() + creditAccount.getLimit() >= money) {
                    // 如果不低于,则进行转账
                    // 1.当前账户余额减少
                    creditAccount.setBalance(creditAccount.getBalance() - money);
                    // 2.目标账户余额增加
                    targetAccount.setBalance(targetAccount.getBalance() + money);
                    System.out.println("提示:恭喜您,向" + id + "账户成功转入" + money + "元");
                } else {
                    // 如果账户余额不足,则报错
                    System.out.println("错误:对不起,您的账户余额不足,转账失败!");
                }
            }

        }
        showMenu();
    }

    public void providerLoan() {
        System.out.println("*************************开始贷款*************************");
        // 只有目标账户属于贷款账户,才能进行贷款
//        LoanSavingAccount loan = new LoanSavingAccount();
//        LoanCreditAccount loan2 = new LoanCreditAccount();
        // 只要是贷款账户,一定实现了Loanable接口
        // 可以通过多态表示贷款账户
//        Loanable loan = new LoanSavingAccount();
//        Loanable loan2 = new LoanCreditAccount();
        // 因此,只需要判断当前账户是否可以向下转型为Loanable接口类型
        // 如果能够向下转型,说明是贷款账户
        if (account instanceof Loanable) {
            System.out.print("请输入贷款金额:");
            double money = sc.nextDouble();
            // 将当前账户转换成贷款接口的引用
            Loanable loanable = (Loanable) account;
            // 执行贷款方法
            loanable.providerLoan(money);
            System.out.println("提示:恭喜您,贷款成功!");
        } else {
            System.out.println("错误:对不起,您的账户不属于贷款账户,无法贷款!");
        }
        showMenu();
    }

    public void payLoan() {
        System.out.println("*************************开始还贷*************************");
        // 还贷功能也必须是贷款账户
        if (account instanceof Loanable) {
            Loanable loanable = (Loanable) account;
            System.out.print("请输入还贷金额:");
            double money = sc.nextDouble();
            // 调用还贷方法
            if (loanable.payLoan(money)) {
                System.out.println("提示:恭喜您,成功还贷" + money + "元!");
            } else {
                System.out.println("错误:对不起,您的账户余额不足,还贷失败!");
            }
        } else {
            System.out.println("错误:对不起,您的账户不属于贷款账户,无法贷款!");
        }
        showMenu();
    }

    public void queryLoan() {
        System.out.println("*************************查询贷款金额*************************");
        // 贷款相关功能均必须是贷款账户
        if (account instanceof Loanable) {
            Loanable loanable = (Loanable) account;
            System.out.println("提示:您当前的贷款金额为:" + loanable.queryLoan() + "元");
        } else {
            System.out.println("错误:对不起,您的账户不属于贷款账户,无法贷款!");
        }
        showMenu();
    }

    public void modifyPassword() {
        System.out.println("*************************修改密码*************************");
        System.out.print("请输入原密码:");
        String oldPassword = sc.next();
        // 判断原密码是否正确
        if (oldPassword.equals(account.getPassword())) {
            // 如果原密码正确,则执行修改密码的操作
            // 密码需要输入两次,且两次输入的密码必须一致
            // 共有三次机会
            int count = 0;
            while (true) {
                System.out.print("请输入新密码:");
                String newPassword = sc.next();
                System.out.print("请再次输入新密码:");
                String newPassword2 = sc.next();
                count++;
                if (newPassword.equals(newPassword2)) {
                    // 如果两次输入密码一致,则密码设置成功
                    account.setPassword(newPassword);
                    System.out.println("提示:恭喜您,密码修改成功!");
                    // 退出主菜单
                    logout();
                    // 退出循环
                    break;
                } else {
                    System.out.println("错误:两次输入的密码不一致,请重新输入!");
                }
                if (count == 3) {
                    System.out.println("错误:错误次数已达上限,请重新操作!");
                    showMenu();
                    break;
                }
            }
        } else {
            System.out.println("错误:您输入的原密码不正确!");
            showMenu();
        }
    }

    public void logout() {
        System.out.println("提示:您已经退出!");
        new Start().showMenu();
    }
}
